package dao.impl;

import dao.UserDao;
import org.example.bazhou.model.User;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User findByUserName(String name) {

        String sql = "select *from user where username = ?";
        User user = new User();

        try (
                Connection conn = DBUtil.getConn();
             PreparedStatement prep = conn.prepareStatement(sql)
        ) {
            prep.setString(1, name);

            try (ResultSet rs = prep.executeQuery()){
                //数据结果集
                while (rs.next()) {
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setName(rs.getString("name"));
                    user.setRole(rs.getString("role"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    user.setCreate_time(
                            rs.getTimestamp("create_time")
                                    .toLocalDateTime()
                    );
                    user.setStatus(rs.getInt("status"));
                }return user;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


}
